#include <gtk/gtk.h>

static void click_a(GtkButton *btn,gpointer user_data){
    const gchar *s;
    s = gtk_button_get_label(btn);
    g_print("%s\n",s);
    if(g_strcmp0(s,"hello")==0){
        
        gtk_button_set_label(btn,"GoodBye");
    }else{
        gtk_button_set_label(btn,"hello");
    }
}

static void click_b(GtkButton *btn,gpointer user_data){
    GtkWindow *win = GTK_WINDOW(user_data);
    //销毁窗体
    gtk_window_destroy(win);
}

static void app_activate(GApplication *app,gpointer user_data){
    GtkWidget *win;
    GtkWidget *box;
    GtkWidget *btn1;
    GtkWidget *btn2;

    win = gtk_application_window_new(GTK_APPLICATION(app));
    //设置窗体的标题
    gtk_window_set_title(GTK_WINDOW(win),"我是标题");
    gtk_window_set_default_size(GTK_WINDOW(win),400,300);

    //添加box
    box = gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
    gtk_box_set_homogeneous(GTK_BOX(box),TRUE);
    
    gtk_window_set_child(GTK_WINDOW(win),box);

    btn1 = gtk_button_new_with_label("hello");
    g_signal_connect(btn1,"clicked",G_CALLBACK(click_a),NULL);

    btn2 = gtk_button_new_with_label("退出");
    g_signal_connect(btn2,"clicked",G_CALLBACK(click_b),win);

    gtk_box_append(GTK_BOX(box),btn1);
    gtk_box_append(GTK_BOX(box),btn2);
    //显示主窗体
    gtk_widget_show(win);
}

int main(int argc,char** argv){
    GtkApplication *app;
    int stat;

    app = gtk_application_new("www.laowan.com",G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL);
    stat =g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);
    return stat;
}